// pages/musiclist/musiclist.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        musicList: "",
        choose: '',
        mark: '',
        val: '',
        ishow: false,//是否选择选择自己的音乐
        customval: "",//显示自定义的链接,
        isEnable: "",//是否启用音乐

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: '背景音乐设置',
        })
        this.getmusiclist();
    },
    //选择自制定
    clickauto: function (e) {
        var _this = this;
        var musicList = _this.data.musicList;
        var ishow = _this.data.ishow;
        musicList.forEach(function (obj, i) {
            obj.ishow = false;
        })

        _this.setData({
            "musicList": musicList,
            "ishow": true,
            "choose": "custom"
        });
    },
    //保存设置
    formSubmit: function (e) {
        var choose = this.data.choose;
        var dataArr = {}
        dataArr.isEnable = e.detail.value.isEnable ? 1 : 0;
        dataArr.choose = choose;
        if (choose == "custom") {
            if (e.detail.value.musicUrl == "") {
                wx.showToast({
                    title: '请粘贴链接',
                    icon: 'none',
                    duration: 2000
                })
                return;
            }
            dataArr.musicUrl = e.detail.value.musicUrl;
        } else {
            dataArr.musicUrl = this.data.val;
        }
        console.log(dataArr)
        app.http.POST("/MiniCardParameters/MyMusicSet", dataArr, (err, data) => {
            console.log(data);
            if (data.code == 1) {
                wx.showToast({
                    title: '设置成功',
                    icon: 'success',
                    duration: 2000,
                    success: function (res) {
                        setTimeout(function () {
                            wx.redirectTo({
                                url: '../mycenter/mycenter'
                            })
                        }, 2000)
                    }
                })
            } else {
                wx.showToast({
                    title: '设置失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })

    },
    //点击选择音乐
    clickmusic: function (e) {
        var _this = this;
        var mark = e.currentTarget.dataset.mark;
        var val = e.currentTarget.dataset.val;
        console.log(mark);
        var musicList = _this.data.musicList;
        var ishow = _this.data.ishow;
        musicList.forEach(function (obj, i) {
            obj.ishow = false;
            if (obj.Mark == mark) {
                obj.ishow = true;
            }
        })
        _this.setData({
            "choose": mark,
            "val": val,
            "musicList": musicList,
            "ishow": false,
            "customval": ''
        });
    },
    //获取音乐列表
    getmusiclist: function () {
        var _this = this;
        wx.showLoading({
            title: '加载中',
        })
        app.http.POST("/MiniCardParameters/GetDefaultMusicList", {}, (err, data) => {

            if (data.code == 1) {
                _this.setData({
                    "musicList": data.data
                })
                app.http.POST("/MiniCardParameters/GetMyMusicSet", {}, (err, data) => {
                    var MusicChoose = data.data.MusicChoose;
                    var musicList = _this.data.musicList;
                    var ishow = _this.data.ishow;
                    var MusicEnable = data.data.MusicEnable;
                    if (MusicEnable == 1) {
                        MusicEnable = true;
                    } else {
                        MusicEnable = false;
                    }
                    if (MusicChoose == "custom") {
                        musicList.forEach(function (obj, i) {
                            obj.ishow = false;
                        })
                        _this.setData({
                            "musicList": musicList,
                            "ishow": true,
                            "choose": MusicChoose,
                            "customval": data.data.musicUrl,
                            "isEnable": MusicEnable
                        });
                    } else {
                        musicList.forEach(function (obj, i) {
                            obj.ishow = false;
                            if (obj.Mark == MusicChoose) {
                                obj.ishow = true;
                            }
                        })
                        _this.setData({
                            "choose": MusicChoose,
                            "val": data.data.musicUrl,
                            "musicList": musicList,
                            "ishow": false,
                            "isEnable": MusicEnable
                        });
                    }
                    console.log(data);
                })
                wx.hideLoading()
            } else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var userInfo = app.globalData.userInfo;
        //判断当前用户是否开通服务,开通则分享美名片页,否则分享访问记录页
        if (userInfo == null || typeof (userInfo) == "undefined") {
            app.getUserall(function (res) {
                userInfo = res;
            });
        }
        var path = userInfo.UserType == 2 ? "/pages/index/index?cid=" + userInfo.Id : "/pages/cardrecord/cardrecord";
        return {
            title: userInfo.NickName + "的美名片",
            path: path,
            success: function (res) {
                wx.showToast({
                    title: '分享成功',
                    icon: "success",
                    duration: 200
                })
            }
        }
    }
})